package com.plian.system.controller.pf;

import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.StringUtil;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.common.status.CustomCode;
import com.plian.system.config.Resubmit;
import com.plian.system.constant.sys.FormTypeConstant;
import com.plian.system.dto.pf.BasePerformanceEffectDTO;
import com.plian.system.entity.pf.AppraisalIndex;
import com.plian.system.entity.pf.BasePerformanceEffect;
import com.plian.system.entity.pf.PerformanceEffectDetail;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.support.Query;
import com.plian.system.node.ForestNodeMerger;
import com.plian.system.service.pf.*;
import com.plian.system.vo.pf.AppraisalIndexVO;
import com.plian.system.vo.pf.BasePerformanceEffectVO;
import com.plian.system.wrapper.pf.AppraisalIndexWrapper;
import com.plian.system.wrapper.pf.BasePerformanceEffectWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.util.List;
import java.util.Optional;

/**
 * @Author: Take-off
 * @Description: TODO 履职成效考评内容 控制器
 * @Date: 2022/12/14 9:36 AM
 * @Version: 1.0
 **/
@RestController
@AllArgsConstructor
@RequestMapping("api/performanceeffect")
@Api(value = "BasePerformanceEffectController", tags = "履职成效考评内容 控制器")
public class BasePerformanceEffectController extends BaseController {

    private final IPerformanceEffectService performanceEffectService;

    private final IPerformanceScoreService performanceScoreService;

    private final BasePerformanceEffectWrapper basePerformanceEffectWrapper;

    private final AppraisalIndexWrapper appraisalIndexWrapper;

    private final IPerformanceScoreChangeService performanceScoreChangeService;

    private final IPerformanceEffectChangeService performanceEffectChangeService;

    private final IBusinessImportService businessImportService;

    @PostMapping(value = "/uploadFile",produces="application/json;charset=UTF-8")
    @ApiOperation(value = "导入", notes = "传入导入文件")
    public R uploadFile(@RequestParam("file") MultipartFile multipartFile){
        businessImportService.uploadFilePerformance(multipartFile);
        return R.success();
    }

    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入 basePerformanceEffect")
    public R<BasePerformanceEffectVO> detail(@RequestBody BasePerformanceEffect basePerformanceEffect) {
        BasePerformanceEffectVO detailObj = performanceEffectService.getDetailObj(basePerformanceEffect);
        if (detailObj.getFormType().equals(FormTypeConstant.PF_PERFORMANCE_EFFECT_CHANGE + "")){
            //变更详情，加上上个版本的详情
            BasePerformanceEffect performanceEffect = new BasePerformanceEffect();
            performanceEffect.setId(detailObj.getLastVersionId());
            BasePerformanceEffectVO lastVO = performanceEffectService.getDetailObj(performanceEffect);
            detailObj.setLastInfoVO(lastVO);
        }
        return R.data(detailObj);
    }

    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入query")
    public ListResult<BasePerformanceEffectVO> list(@RequestBody Query query) {
        ListResult<BasePerformanceEffectVO> listResult = performanceEffectService.list(query);
        return listResult;
    }

    @PostMapping("/submit")
    @ApiOperation(value = "新增或修改", notes = "basePerformanceEffect")
    @Resubmit(delaySeconds = 0)
    @LogOperation("履职成效考评内容-提交")
    public R<BasePerformanceEffect> submit(@Valid @RequestBody BasePerformanceEffect basePerformanceEffect) {

        if (!Optional.ofNullable(basePerformanceEffect.getId()).isPresent()){
            String checkMessage = performanceEffectService.checkYear(basePerformanceEffect);
            if (StringUtil.isNotBlank(checkMessage)){
                return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
            }
        }

        boolean result = performanceEffectService.submit(basePerformanceEffect);

        return result ? R.data(basePerformanceEffect) : new R(CommonCode.FAIL);
    }

    @PostMapping("/submit-change")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "新增或修改-考评内容-变更信息", notes = "basePerformanceEffect")
    @LogOperation("履职成效考评内容-变更")
    public R<BasePerformanceEffect> submitChange(@Valid @RequestBody BasePerformanceEffect basePerformanceEffect) {
        boolean result = performanceEffectChangeService.submit(basePerformanceEffect);

        return result ? R.data(basePerformanceEffect) : new R(CommonCode.FAIL);
    }

    @PostMapping("/remove")
    @ApiOperation(value = "删除", notes = "传入 ids")
    @LogOperation("履职成效考评内容-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>)pd.get("ids");
        return performanceEffectService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA)))
                ? new R(CommonCode.SUCCESS) : new R(CommonCode.FAIL);
    }

    @PostMapping("/detailScore")
    @ApiOperation(value = "详情-考核评分", notes = "传入 basePerformanceEffect")
    public R<BasePerformanceEffectVO> detailScore(@RequestBody BasePerformanceEffect basePerformanceEffect) {
        BasePerformanceEffectVO detailObj = performanceScoreService.getDetailObj(basePerformanceEffect);
        if (detailObj.getFormType().equals(FormTypeConstant.PF_PERFORMANCE_SCORE_CHANGE + "")){
            //变更详情，加上上个版本的详情
            BasePerformanceEffect performanceEffect = new BasePerformanceEffect();
            performanceEffect.setId(detailObj.getLastVersionId());
            BasePerformanceEffectVO lastVO = performanceScoreService.getDetailObj(performanceEffect);
            detailObj.setLastInfoVO(lastVO);
        }

        return R.data(detailObj);
    }

    @PostMapping("/submitScore")
    @ApiOperation(value = "新增或修改-考核评分", notes = "basePerformanceEffect")
    @Resubmit(delaySeconds = 0)
    @LogOperation("履职成效考评内容-提交考核评分")
    public R<BasePerformanceEffect> submitScore(@Valid @RequestBody BasePerformanceEffect basePerformanceEffect) {
        if (!Optional.ofNullable(basePerformanceEffect.getId()).isPresent()){
            String checkMessage = performanceScoreService.checkYear(basePerformanceEffect);
            if (StringUtil.isNotBlank(checkMessage)){
                return new R(CustomCode.builder().message(checkMessage).code(1999).success(false).build());
            }
        }
        boolean result = performanceScoreService.submit(basePerformanceEffect);
        return result ? R.data(basePerformanceEffect) : new R(CommonCode.FAIL);
    }

    @PostMapping("/submit-score-change")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "新增或修改-考核评分-变更信息", notes = "basePerformanceEffect")
    @LogOperation("履职成效考评内容-变更考核评分")
    public R<BasePerformanceEffect> submitScoreChange(@Valid @RequestBody BasePerformanceEffect basePerformanceEffect) {
        boolean result = performanceScoreChangeService.submit(basePerformanceEffect);

        return result ? R.data(basePerformanceEffect) : new R(CommonCode.FAIL);
    }

    @PostMapping("/effectDetail")
    @ApiOperation(value = "考评内容列表", notes = "传入 dto")
    public ListResult<PerformanceEffectDetail> getEffectDetails(@RequestBody BasePerformanceEffectDTO dto){
        List<PerformanceEffectDetail> effectDetails = performanceEffectService.getEffectDetails(dto);

        ListResult<PerformanceEffectDetail> listResult = new ListResult<>(CommonCode.SUCCESS);
        listResult.setList(effectDetails);
        listResult.setTotal(effectDetails.size());
        return listResult;
    }

    @PostMapping("/listForTree")
    @ApiOperation(value = "树状返回", notes = "传入query")
    public ListResult<AppraisalIndexVO> listForTree() {
        List<AppraisalIndex> lastThreeIndex = performanceScoreService.getLastThreeIndex();
        List<AppraisalIndexVO> merge = ForestNodeMerger.merge(appraisalIndexWrapper.entityToVO(lastThreeIndex));
        ListResult<AppraisalIndexVO> result = new ListResult<>(CommonCode.SUCCESS);
        result.setList(merge);
        result.setTotal(merge.size());
        return result;
    }

    @PostMapping("/version-list")
    @ApiOperation(value = "查看版本", notes = "传入 initialId")
    public ListResult<BasePerformanceEffectVO> versionList(@RequestBody Query query) {
        return performanceScoreService.versionList(query);
    }

}
